package com.weiyuwqj.controller.item;

import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.vo.BasItemVo;
import com.weiyuwqj.api.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2016/11/24.
 * 项目列表，项目详情
 */
@Controller
public class ItemDetailController {

    @Autowired
    BasItemService itemService;
    @Autowired
    BasUserSecurityService userSecurityService;
    @Autowired
    SysPictureService sysPictureService;
    @Autowired
    BasUserInfoService userInfoService;
    @Autowired
    BusInvestService investService;
    @Autowired
    BusAccountService accountService;
    @Autowired
    BusInvestRepayService investRepayService;
    @Autowired
    BasUserService userService;


    /**
     * 项目详情
     * @param itemId 项目id
     * @return
     */
    @RequestMapping(value = "/item/details/{itemId}")
    public Object documents(@PathVariable Integer itemId,HttpServletRequest request) {
        int id = itemId;
        //查询项目信息
        BasItem item = itemService.selectByKey(id);
        //查询借款人实名信息
        BasUserSecurity security = userSecurityService.getOneByUserId(item.getItemUserId());
        Integer age=35;//StringUtils.IdNOToAge(security.getIdentifyCard());
        //将身份证，姓名转义*
        security.setIdentifyCard(StringUtils.getString(security.getIdentifyCard()));
        security.setRealname(StringUtils.getString(security.getRealname()));

/*        //1-现金贷，2-房贷，3-车贷
        if (item.getItemType() == 1) {
            Map repayMap = investRepayService.countRepayNum(item.getItemUserId());
            request.getSession().setAttribute("repayNum", repayMap);
        } else if (item.getItemType() == 2) {
            BusItemHouse itemHouse = itemHouseService.getHouseByItemId(itemId);
            request.getSession().setAttribute("itemHouse", itemHouse);
        } else if (item.getItemType() == 3) {
            BusItemLoan itemLoan = itemLoanService.getOneByItemId(itemId);
            request.setAttribute("itemLoan", itemLoan);
        }*/
        //借款人信息
        BasUserInfo info = userInfoService.getOneByUserId(item.getItemUserId());
        //图片列表
        Map param = new HashMap();
        param.put("itemId", itemId);
        param.put("pictureType", 4);
        List pictureList = sysPictureService.getPictureByItemId(param);
        //投资用户资产信息
        Integer userId=(Integer)request.getSession().getAttribute("userId");
        if(userId != null){
            request.getSession().setAttribute("user", accountService.getBusAccountByUserId(userId));
        }

        //查询借款人信息
       BasUser user = userService.selectByKey(item.getItemUserId());
        if(user!=null){
//            String area = phoneService.getAddressByMobile(user.getMobile());
//            request.setAttribute("area", area);
            if(info!=null&&info.getCurrentAddress()==null){
                info.setCurrentAddress("This is Hangzhou for testing"/*phoneService.getAddressByMobile(user.getMobile())*/);
                userInfoService.updateNotNull(info);
            }
        }
        request.setAttribute("item", item);
        request.setAttribute("security", security);
        request.setAttribute("pictureList", pictureList);
        request.setAttribute("info", info);
        request.setAttribute("age", age);
        return "main/investment/details";

    }

    /**
     * 项目投资列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/item/investList")
    public Object getList(Paginator p,HttpServletRequest request){
        String itemId=request.getParameter("itemId");
        Map map=new HashMap();
        map.put("itemId",itemId);
        PageList list=investService.listInvest(p, map);
        return list;
    }

    /**
     * 项目图片列表
     */
    @ResponseBody
    @RequestMapping(value = "/item/pictureList")
    public Object pictureList(Integer itemId){
        Map param = new HashMap();
        param.put("itemId", itemId);
        param.put("pictureType", 4);
        List pictureList = sysPictureService.getPictureByItemId(param);
        return pictureList;
    }
    /**
     * 还款计划
     */
    @ResponseBody
    @RequestMapping(value = "/item/repayList")
    public Object repayList(Paginator p,Integer itemId){
        Map param = new HashMap();
        param.put("itemId", itemId);
        //PageList list=investRepayService.getItemRepayList(p,param);
        return null;
    }

}
